package com.zhu.test;

import lombok.Data;
import org.junit.Test;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2022/02/25/14:51
 * @Description:reduce方法测试Demo
 */
public class ReduceDemo {
    @Test
    public void reduceTest1() {
        Optional accResult = Stream.of(1, 2, 3, 4).reduce((acc, item) -> {
            System.out.println("acc : " + acc);
            acc += item;
            System.out.println("item: " + item);
            System.out.println("acc+ : " + acc);
            System.out.println("--------");
            return acc;
        });
        System.out.println(accResult);
    }

    @Test
    public void reduceTest() {
        Integer integer = Stream.of(1, 2, 3, 4).reduce(1,Integer::sum);
        System.out.println(integer);
    }

    /*
    * 对基本数据类型，在 finally 块中改变 return 的值没有任何影响，直接覆盖掉；
    * ⽽对引⽤类型是有影响的，返回的是在 finally 对 前⾯ return 语句返回对象的修改值
    * */
    @Test
    public void exceptionTest() {
        Student i = tryTest();

        System.out.println( "i: " + i);
    }

    public Student tryTest(){
//        int i = 0;
//        String i = new String("");
        Student stu = new Student();
        try{
            stu.name = "zhu";
            stu.age = 1;
            return stu ;
        }catch (Exception e){
            stu.age = -1;
            return stu ;
        }finally {
            stu.name = "lei";
            stu.age = 100;
        }
    }

    @Data
    class Student{
        public String name;
        public int age;
    }
}
